// INCLUDES --------------------------------------------------------------------
#include "Ray.h"

// FUNCTIONS -------------------------------------------------------------------

/* GENERAL:
   d    -> destination
   s    -> source
   size -> array size (0,1 for single elements)

   r    -> arbitrary ray
   t    -> floating point scalar
*/

// clear to o:[0,0,0,1], d:[0,0,-1,1]
void RayClear(Ray *r, ui32 size)
{
  GUARD_NULL("RayClear *r", r);
  
  // clamp range to at least one element
  CLAMP_LOW(size, 1); 

  // clear to o:[0,0,0,1], d:[0,0,-1,1]

  // TODO: address performance penalty by overhead Vector4Clear() calls
  i32 i;
  for (i = 0; i < (i32)size; i++)
  {
    Vector4Clear(&r[i].c[0], 2); // clear o,d to [0,0,0,1]
    r[i].d.z = -1.0f;            // set d:[0,0,-1,1]
  }
};

// clone ray src->dest
void RayCopy(Ray *d, Ray *s, ui32 size)
{
  // copy contents
  FillCopy(d, s, sizeof(Ray), size);
};

/* the vector equation of a line through some point o and parallel to
   another vector d is defined as:

   p = o + td

   where:

   o -> is the origin of the line
   t -> being a scalar
   d -> a vector parallel to the line

   resulting in every real value of scalar t generating a real point p on the line.
*/

// retrieve an arbitrary point p on the line produced by scalar t
void RayPoint(Vector4* d, Ray* r, fp t)
{
	GUARD_NULL("RayPoint *r", r);
	
  // d, the resulting vector -> represents point p

	Vector4Mul(d, &r->d, t); // p = t*direction
	Vector4AddTo(d, &r->o);  // p += origin
};
